package com.wise.controller.client;

import com.alibaba.fastjson.JSONObject;
import com.google.common.util.concurrent.RateLimiter;
import com.wise.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/client")
public class ClientController {
    private static final Logger logger = LoggerFactory.getLogger(ClientController.class);

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getForEntities")
    public String getForEntities() {
        //方式一
        /*ResponseEntity<List> responseEntity = restTemplate.getForEntity("http://localhost:8080/users", List.class);
        HttpHeaders headers = responseEntity.getHeaders();
        HttpStatus statusCode = responseEntity.getStatusCode();
        int value = statusCode.value();
        System.out.println("status:"+value);
        List body = responseEntity.getBody();
        List<User> userList = new ArrayList<>();
        for (int i = 0; i< body.size(); i++){
            String s = JSONObject.toJSONString(body.get(i));
            JSONObject jsonObject = JSONObject.parseObject(s);
            User user = JSONObject.toJavaObject(jsonObject, User.class);
            userList.add(user);
        }
        userList.forEach(System.out::println);*/
        //方式二
        List list = restTemplate.getForObject("http://localhost:8080/users", List.class);
        return list.toString();
    }

    @GetMapping("/getForEntity/{id}")
    public User getForEntityById(@PathVariable(value = "id") String id) {
        //方式一
        /*ResponseEntity<User> responseEntity = restTemplate.getForEntity("http://localhost:8080/users/{id}", User.class, id);
        HttpHeaders headers = responseEntity.getHeaders();
        HttpStatus statusCode = responseEntity.getStatusCode();
        User user = responseEntity.getBody();
        System.out.println("status:"+responseEntity.getStatusCodeValue());
        System.out.println(user);*/
        //方式二
        /*User user = restTemplate.getForObject("http://localhost:8080/users/{id}", User.class, id);*/
        //方式三
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        User user = restTemplate.getForObject("http://localhost:8080/users/{id}", User.class, map);
        return user;
    }

    @GetMapping("/saveUser")
    public String saveUser() {
        User user = new User();
        user.setName("曹操");
        user.setAddr("北魏");
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:8080/users", user, String.class);
        return responseEntity.getBody();
    }

    // 有参数的 postForEntity 请求
    @RequestMapping("/saveUserByType/{type}")
    public String save2(@PathVariable("type") String type) {
        User user = new User();
        user.setName("小北");
        user.setAddr("多冷啊，我在东北玩泥巴");
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:8080/users/{type}", user, String.class, type);
        String body = responseEntity.getBody();

        return body;

    }

    // 有参数的 postForEntity 请求,使用map封装
    @RequestMapping("/saveUserByType2/{type}")
    public String save3(@PathVariable("type") String type) {
        User user = new User();
        user.setName("小南");
        user.setAddr("我在大连没有家");
        HashMap<String, String> map = new HashMap<>();
        map.put("type", type);

        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:8080/users/{type}", user, String.class, map);
        String body = responseEntity.getBody();

        return body;

    }

    @RequestMapping("/tokenTest")
    public String batchRequest() {
        Map<String, String> map = new HashMap<>();
        map.put("id", "1");
        //每秒向桶中放入一个令牌
        RateLimiter rateLimiter = RateLimiter.create(1);
        for (int i = 0; i < 10; i++) {
            double acquire = rateLimiter.acquire();
            logger.info("获取令牌成功!阻塞时间:" + acquire);
            User user = restTemplate.getForObject("http://localhost:8080/users/{id}", User.class, map);
            logger.info("获取到的数据为:" + JSONObject.toJSONString(user));
        }
        return "测试结束";
    }
}
